/* AirSortReportService.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   apep	2010-1-27	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2010
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.apep.jwcs.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.apep.jwcs.dao.AirSortReportDao;
import com.apep.jwcs.dao.BusInfoDao;
import com.apep.jwcs.dao.OutSubViewDao;
import com.apep.jwcs.pojo.AirSortReport;
import com.apep.jwcs.pojo.BusInfo;
import com.apep.jwcs.pojo.OutSubView;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.bean.GSOutHeadBean;
import com.hitrust.gs.bean.GSOutSubBean;
import com.hitrust.gs.util.exception.GSException;


public class AirSortReportService  {
    
    public Map getAirSortReportMap(String warehouseCode,String startTime,String endTime) throws GSException{
        Map  brandMap=new HashMap();
        OutSubViewDao outSubViewDao = new OutSubViewDao();
        List outSubViewList=outSubViewDao.findOutSubView(startTime, endTime, warehouseCode);
        if(outSubViewList.size()>0){
            for (int i = 0; i < outSubViewList.size(); i++) {
                OutSubView view =(OutSubView) outSubViewList.get(i);
                AirSortReport airSortReport=new AirSortReport();
                //获取车辆上牌时间
                Date date=view.getBusUseDate();
                //获取出库时间
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                Date outTime=new Date();
                try {
                    outTime = format.parse(view.getOutTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //产品分类
                String category=view.getProductCategoryName();
                //产品品牌
                String productBrank = view.getProductBrank();
                double money=view.getProductMoney();
                double mon=0;
                double fiveType = 0;
                double fiveTypeChange = 0;
                double NoFiveType = 0;
                double noFiveTypeChange = 0;
                int count=0;
                if(category.equals("五大类")){
                     count=getAirSortCount(date,outTime,1);
                     mon=allMoney(money,count);
                     //自付金额判断不为六年保内,并且不为三年保
                     if(count!= 1 && count != 2){
                         fiveTypeChange = mon;
                     }
                     fiveType=money;
               }else if(category.equals("非五大类")){
                     count=getAirSortCount(date,outTime,2);
                     mon=allMoney(money,count);
                     //自付金额判断不为六年保内,并且不为三年保
                     if(count!= 1 && count != 2){
                         noFiveTypeChange = mon;
                     }
                     NoFiveType=money;
               }else if(category.equals("易损件")){
                     count=getAirSortCount(date,outTime,3);
                     mon=allMoney(money,count);
               }else{
                   continue;
               }
               //判断brandList是否有值
               if(brandMap.size()>0){
                     //判断brandList里的产品品牌是否重复
                     if(brandMap.containsKey(productBrank)){
                          AirSortReport airSortReport2= (AirSortReport) brandMap.get(productBrank);
                          //给封装类 根据相应属性赋值
                          airSortReport2.setExpandFiveType(airSortReport2.getExpandFiveType()+fiveType);
                          airSortReport2.setExpandNoFiveType(airSortReport2.getExpandNoFiveType() + NoFiveType);
                          airSortReport2.setExpandFiveTypeChange(airSortReport2.getExpandFiveTypeChange() + fiveTypeChange);
                          airSortReport2.setExpandNoFiveTypeChange(airSortReport2.getExpandNoFiveTypeChange() + noFiveTypeChange);
                          setMoneyByCount(count,mon,airSortReport2);
                          brandMap.put(productBrank,airSortReport2);
                     }else{
                         //给封装类 根据相应属性赋值
                         setMoneyByCount(count,mon,airSortReport);
                         airSortReport.setProductBrand(productBrank);
                         airSortReport.setExpandFiveType(fiveType);
                         airSortReport.setExpandNoFiveType(NoFiveType);
                         airSortReport.setExpandFiveTypeChange(fiveTypeChange);
                         airSortReport.setExpandNoFiveTypeChange(noFiveTypeChange);
                         brandMap.put(productBrank, airSortReport);
                     }
               }else{
                    //给封装类 根据相应属性赋值
                    setMoneyByCount(count,mon,airSortReport);
                    airSortReport.setProductBrand(productBrank);
                    airSortReport.setExpandFiveType(fiveType);
                    airSortReport.setExpandNoFiveType(NoFiveType);
                    airSortReport.setExpandFiveTypeChange(fiveTypeChange);
                    airSortReport.setExpandNoFiveTypeChange(noFiveTypeChange);
                    brandMap.put(productBrank, airSortReport);
               }     
            }
        }
        return brandMap;
    }
    
    /**
     * 判断质保期
     * @param busDate 车辆上牌日
     * @param outDate   出库时间
     * @param type,1:五大类，2：非五大类，3：易损件
     * @return 质保类型 1：6年保内，2：3年保内，,3:3+3年外保（五大类),4:3+3年外保（非五大类）,5：6年保 6年质保外,6:易损件，7:3+3 6年质保后
     */
    public int getAirSortCount(Date busDate,Date outDate,int type){
        if(type==3){
            return 6;
        }
        //判断是否为6年保
        if(isSixYear(busDate)){
            //判断是否在6年内
            if(isDateInSide(busDate, outDate, 6)){
                return 1;
            }else{
                return 5;
            }
        }else{
            //为3+3保，需判断是否为5大类
            //判断3+3是否超过6年
            if(isDateInSide(busDate, outDate, 6)){
                //是否在保修3年内
                if(isDateInSide(busDate,outDate,3)){
                    return 2;
                }else{
                    if(type==1){
                      return 3;
                  }else{
                      return 4;
                  }
                }
            }else{
                return 7;
            }
        }
    }

    /**
     * @param date
     * @return
     */
    private int getDateInteger(Date date,String type) {
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        String dateStr = sdf.format(date);
        return Integer.parseInt(dateStr);
    }
    
    /**
     * 用减法对比年
     * return 1:出库时间减去出厂时间大于指定年数 2:dateTwo=dateOne 3:dateTwo<dateOne
     */
    private int comparetoDate(Date dateOne,Date dateTwo,int yearNumber,String type){
        if(getDateInteger(dateTwo, type)-getDateInteger(dateOne, type)>yearNumber){
            return 1;
        }else if(getDateInteger(dateTwo, type)-getDateInteger(dateOne, type)==yearNumber){
            return 2;
        }else {
            return 3;
        }
    }

    /**
     * 用比较对比（dateTwo是否大于dateTwo）
     * @param dateOne
     * @param dateTwo
     * @param type
     * @return
     */
    private boolean comparetoType(Date dateOne,Date dateTwo,String type){
        if(getDateInteger(dateTwo, type)>getDateInteger(dateOne, type)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 是否是6年保
     * @param date
     * @return
     */
    private boolean isSixYear(Date date){
        int year = getDateInteger(date, "yyyy");
        int month = getDateInteger(date, "MM");
        int day = getDateInteger(date, "dd");
        boolean flag = false;
        if(year>=2009){
            if(month>=6){
                if(day>=1){
                    flag = true;
                }
            }
        }
        return flag;
    }
    
    /**
     * 是否在指定时间内
     * @param dateOne
     * @param dateTwo
     * @param yearNumber
     * @return
     */
    private boolean isDateInSide(Date dateOne,Date dateTwo,int yearNumber){
        boolean flag =false;
        switch (comparetoDate(dateOne, dateTwo, yearNumber, "yyyy")) {
            case 1:flag =false;break;
            case 2: switch (comparetoDate(dateOne, dateTwo, 0, "MM")){
                            case 1:flag=false;break;
                            case 2: switch (comparetoDate(dateOne, dateTwo, 0, "dd")){
                                        case 1:flag=false;break;
                                        case 2:flag=true ;break;
                                        case 3:flag=true;break;
                                    }
                                    break;
                            case 3:flag=true;break;
                            }
                    break;
            case 3:flag =true; break;
        }
        return flag;
    }
    
    /**
     * 根据判断保质期计算应付金额
     * @param money  总金额
     * @param airSortCount 1：6年保，2：3年保内，,3:3+3年外保（五大类),4:3+3年外保（非五大类）,5：6年保 6年质保外,6:易损件，7:3+3 6年质保后
     * @return
     */
    public double allMoney(double money,int airSortCount){
        double mon= money;
        switch (airSortCount) {
        case 1:
            mon= money;break;
        case 2:
            mon= money;break;
        case 3:
            mon= money*0.2;break;
        case 4:
            mon= money;break;
        case 5:
            mon= money;break;
        case 6:
            mon=money;break;
        case 7:
            mon=money;break;
        }
        return mon;
    }
    
    /**
     * 封装类相应编号加钱
     * @param count 质保期类型
     * @param money 已经被做过算法的 钱
     * @param airSortReport
     */
    public void setMoneyByCount(int count,double money,AirSortReport airSortReport){
        switch (count) {
        case 1:
            airSortReport.setExpendSix(airSortReport.getExpendSix()+money);break;
        case 2:
            airSortReport.setExpandThree(airSortReport.getExpandThree()+money);break;
        case 3:
            airSortReport.setExpandThreeInSort(airSortReport.getExpandThreeInSort()+money) ;break;
        case 4:
            airSortReport.setExpandThreeNoSort(airSortReport.getExpandThreeNoSort()+money);break;
        case 5:
            airSortReport.setExpandSixOutside(airSortReport.getExpandSixOutside()+money);break;
        case 6:
            airSortReport.setExpandQuickWearPart(airSortReport.getExpandQuickWearPart()+money);break;
        case 7:
            airSortReport.setExpandThreeOutside(airSortReport.getExpandThreeOutside()+money);break;
        }
    }
}
